#!/bin/bash

if [ ! -d  "${HOME}/logbak" ] ; then
	MKDIRLOGBAK="mkdir ${HOME}/logbak"
	echo ${MKDIRLOGBAK}
	${MKDIRLOGBAK}
fi

if [ ! -d "${HOME}/log" -o  ! -d "${HOME}/logbak" ] ; then
	echo "log or logbak not exist"
	exit 1;
fi

DATETIME=`/bin/date +%Y%m%d_%H%M%S`
TAR_PATHFILENAME="${HOME}/logbak/log_${DATETIME}.gz.tar"

if [ ! -d  "${HOME}/logbak/logbak_${DATETIME}" ] ; then
	mkdir ${HOME}/logbak/logbak_${DATETIME}
	if [ $? -ne 0 ] ; then
		echo "*** ERROR : disk space is not enough"
		exit 1;
	fi
fi

cd ${HOME}/log
find . -type f -name "*.log*" -exec mv {} ${HOME}/logbak/logbak_${DATETIME} \;

cd ${HOME}/logbak/logbak_${DATETIME}
find . -type f -name "*.log*" | while read -r LINE
do
	GZIPLINE="gzip ${LINE}"
	echo ${GZIPLINE}
	${GZIPLINE}
	if [ $? -ne 0 ] ; then
		echo "*** ERROR : disk space is not enough"
		rm -f ${LINE}.gz
#		gunzip *.gz
		exit 1;
	fi
done

cd ${HOME}/logbak
tar -cvf ${TAR_PATHFILENAME} logbak_${DATETIME}
if [ $? -ne 0 ] ; then
	echo "*** ERROR : disk space is not enough"
	rm -f ${TAR_PATHFILENAME}
	exit 1;
fi
rm -rf logbak_${DATETIME}

